// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  if (event.type == "seek") {
    return await db.collection('answer').where({
      name: db.RegExp({
        regexp: event.seekValue,
        options: 'i',
      })
    }).get()
  } else if (event.type == "add") {

    await db.collection('gods').where({
      _openid: wxContext.OPENID,
    }).update({
      data: {
        btList: _.push({
          name: "上传教材答案",
          time: new Date(),
          num: 5
        })
      }
    })
    return await db.collection('answer').add({
      data: {
        time: new Date(),
        name: event.name,
        path: event.path,
        _openid: wxContext.OPENID
      }
    })
  } else if (event.type == "in") {
    return await db.collection('answer').limit(10).get()
  }
}